<template>
  <div class="tool-container">
    <el-form class="tool-search" :model="queryParams" ref="queryFormRef" :inline="true">
      <el-form-item prop="name">
        <el-input v-model="queryParams.stationName" placeholder="房站名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item prop="regionCode">
        <RegionSelect v-model="queryParams.regionCode" placeholder="区域" clearable />
      </el-form-item>
      <el-form-item prop="status">
        <DictSelect dict="station_status" v-model="queryParams.status" placeholder="状态" clearable />
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="times"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="YYYY-MM-DD hh:mm:ss"
          value-format="YYYY-MM-DD hh:mm:ss"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div class="tabel-container">
    <vxe-grid ref="tableRef" v-bind="gridOptions">
      <template #epayType="{ row }">
        <dict-tag :options="epay_type" :value="row.ePayType" />
      </template>
      <template #handle="{ row }">
        <el-button link type="primary" @click="handleDetail(row)" v-hasPermi="['system:tenant:edit']">详情</el-button>
        <el-button link type="primary" @click="handleUpdate(row)" v-hasPermi="['system:tenant:edit']">编辑</el-button>
      </template>
      <template #bottom>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
      </template>
    </vxe-grid>
  </div>
</template>

<script setup lang="ts">
import { listLeak } from '@/api/station/leak/index';
import { BizStationLeakDetectVo } from '@/api/station/leak/types';
import { getTableConfig } from './columns';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { epay_type } = toRefs<any>(proxy?.useDict('epay_type'));
const gridOptions = reactive(getTableConfig());
const data = toRef(gridOptions, 'data');
const loading = toRef(gridOptions, 'loading');

const total = ref(0);

const queryFormRef = ref<ElFormInstance>();

const queryParams = ref<PageQuery>({
  pageNum: 1,
  pageSize: 20,
  stationName: '',
  status: '',
  regionCode: '',
  startFrom: '',
  endTo: ''
});

const times = computed({
  get: () => {
    return [queryParams.value.startFrom, queryParams.value.endTo];
  },
  set: (v) => {
    queryParams.value.startFrom = v[0];
    queryParams.value.endTo = v[0];
  }
});

/** 查询租户列表 */
const getList = async () => {
  loading.value = true;
  const res = await listLeak(queryParams.value);
  data.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 搜索重置操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};
/** 新增按钮操作 */
const handleAdd = () => {};

/** 修改按钮操作 */
const handleUpdate = async (row?: BizStationLeakDetectVo) => {
  // proxy?.$tab.openPage('/station/stationMessage/edit', '', { id: row?.stationId })
};
/** 详情按钮操作 */
const handleDetail = async (row?: BizStationLeakDetectVo) => {
  proxy?.$tab.openPage('/station/energyDetail', '', { id: row?.stationId });
};

onMounted(() => {
  getList();
});
</script>
